package com.baseLearn.learn.designPatterns.proxy.jdkDynamicProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class LawyerFactory2 implements InvocationHandler {

    private Person person;

    //维护一个目标对象(被代理对象）
    public LawyerFactory2 help(Person person) {
        this.person = person;
        return this;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("111");
        Object object = method.invoke(person, args);
        System.out.println("2222");
        return object;
    }

    //生成代理对象
    public Object getInstance() {
        return Proxy.newProxyInstance(person.getClass().getClassLoader(), person.getClass().getInterfaces(), this);
    }
}
